package cn.kgc.config;
//rabbitmq配置类
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    //队列名称
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    //交换机名称
    public static final String EXCHANGE_TOPICS_INFORN = "exchange_topics_inform";

    /**
     * exchange_topics_inform
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct.topic，header交换机类型的配置
     *
     * @return
     */
    @Bean(EXCHANGE_TOPICS_INFORN)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        // durable(true)持久化，消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORN).durable(true).build();
    }

    //声明队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SHS() {
        Queue queue = new Queue(QUEUE_INFORM_SMS);
        return queue;
    }

    //声明队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL() {
        Queue queue = new Queue(QUEUE_INFORM_EMAIL);
        return queue;
    }


    /**
     * channel.queueBind (INFORM_QUEUE_SNS , "inform_exchange_topic" , "inform . #  sms .#"");
     * 绑定队列到交换机﹒
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding BINDING_QUEUE_INFORN_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORN) Exchange exchange) {
                                        //绑定交换机 ----bindingKey:inform.#.sms.#
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.sms.#").noargs();
    }

    @Bean
    public Binding BINDIN6_QUEUE_INFORM_ENAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORN) Exchange exchange) {
                                        //绑定交换机 ----bindingKey:inform.#.email.#
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.email.#").noargs();
    }

}
